/******************************************************************************
 * Copyright 2020-xxxx xxx Co., Ltd.
 * All right reserved. See COPYRIGHT for detailed Information.
 *
 * @file       MainWindow.cpp
 * @brief      XXXX Function
 *
 * @author     shibiaoshen
 * @date       2025/04/21
 * @history
 *****************************************************************************/
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QThread>
#include <QDebug>
#include <iostream>
#include <math.h>
#include <cmath>
#include <random>
using namespace std;

using namespace std;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using std::vector;


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/logo/logo.ico"));
    setWindowTitle(u8"雷达上位机");
    setWindowState(Qt::WindowMaximized);//设置窗口最大化


    QStringList headTable = {"速度","方位","距离m","类型"};
    ui->tableWidget->horizontalHeader()->setVisible(true);
    ui->tableWidget->setHorizontalHeaderLabels(headTable);
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setRowCount(50);
    ui->tableWidget->setColumnWidth(0,20);
    ui->tableWidget->setColumnWidth(1,20);
    ui->tableWidget->setColumnWidth(2,20);
    ui->tableWidget->setColumnWidth(3,20);
    QFont font("song", 8, QFont::Normal);
    ui->tableWidget->horizontalHeader()->setFont(font);       //设置字体
    ui->tableWidget->verticalHeader()->setHidden(true);       //隐藏行标题
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableWidget_2->horizontalHeader()->setVisible(true);
    QStringList headTable2={"批号","速度","方位","距离","状态","类型"};
    ui->tableWidget_2->setHorizontalHeaderLabels(headTable2);
    ui->tableWidget_2->setColumnCount(6);
    ui->tableWidget_2->setRowCount(50);
    QFont font_2("song", 8, QFont::Normal);
    ui->tableWidget_2->horizontalHeader()->setFont(font_2);       //设置字体
    ui->tableWidget_2->verticalHeader()->setHidden(true);       //隐藏行标题
    ui->tableWidget_2->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    //设置远程主机的IP和端口号
    QHostAddress ip = QHostAddress("127.0.0.1");
    int port = 5001;
    m_radarController = new RadarController(ip, port, this);
    //     ui->plainTextEdit->setPlainText("plain text test");

    connect(m_radarController,&RadarController::sendMessage,[=](QString str){
        ui->plainTextEdit->appendPlainText(str);
    });



    //初始化雷达界面
    m_radar = new Radar();
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_radar);

    ui->widget->setLayout(layout);

    //m_radarController与m_radar进行通信
    connect(m_radarController,&RadarController::sendPoints,m_radar,&Radar::recvData);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_setParm_clicked()
{
    RadarConfigParm parm;
    parm.freq = 0;
    parm.speed = 2;
    parm.cfar = 0;
    parm.rcs = 0;

    m_radarController->setConfigParm(parm);
}

void MainWindow::on_pushButton_getParm_clicked()
{
    //m_radarController->login();
    m_radarController->getConfigParm();
}

void MainWindow::on_pushButton_getRtk_clicked()
{
    if (ui->pushButton_getRtk->text() == "获取RTK")
    {
        m_radarController->getRTK(true);
        ui->pushButton_getRtk->setText("停止");
    }
    else if (ui->pushButton_getRtk->text() == "停止")
    {
        m_radarController->getRTK(false);
        ui->pushButton_getRtk->setText("获取RTK");
    }

}


void MainWindow::on_pushButton_run_clicked()
{
    if (ui->pushButton_run->text() == "启动")
    {
        m_radarController->run(WorkMode::CirSweep2s);
        ui->pushButton_run->setText("停止");
        ui->plainTextEdit->appendPlainText("上位机启动");
    }
    else if (ui->pushButton_run->text() == "停止")
    {
        m_radarController->run(WorkMode::StandBy);
        ui->pushButton_run->setText("启动");
         ui->plainTextEdit->appendPlainText("上位机关闭");
    }
}




void MainWindow::on_action_triggered()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_set_triggered()
{
    ui->stackedWidget->setCurrentIndex(0);
}








void MainWindow::plotTracks(
        QPainter &painter,
        const vector<TrackDataOutputPackage> &track_data_output_pack_list) {
    int x;
    int y;
    VectorXd trackGUI;  //临时变量，存放转换到GUI上的坐标信息
    QColor qcolor_round_shi(220, 20, 60);  //实点的颜色 红色
    painter.setPen(QPen(qcolor_round_shi, 2, Qt::DashDotLine, Qt::RoundCap));

    size_t N = track_data_output_pack_list.size();
    for (size_t n = 0; n < N; ++n) {
        if (track_data_output_pack_list[n].raw_track_data_output_(3) ==
                0) {  //说明是实点
            trackGUI = tools1.RadarPolarToGUI(
                        track_data_output_pack_list[n].track_dataout_point_);
            x = static_cast<int>(trackGUI(0));
            y = static_cast<int>(trackGUI(1));
            painter.drawPoint(x, y);
        } else {
            QColor qcolor_round_bu(0, 0, 255);  //补点的颜色 蓝色
            painter.setPen(QPen(qcolor_round_bu, 2, Qt::DashDotLine, Qt::RoundCap));
            trackGUI = tools1.RadarPolarToGUI(
                        track_data_output_pack_list[n].track_dataout_point_);
            x = static_cast<int>(trackGUI(0));
            y = static_cast<int>(trackGUI(1));
            painter.drawPoint(x, y);
        }
    }
}

void MainWindow::plotMeasurements(QPainter &painter, const std::vector<MeasurementPackage> &measurement_pack_list)
{

    int x;
    int y;
    VectorXd measurementGUI;  //临时变量，存放转换到GUI上的坐标信息
    QColor qcolor_round(255, 255, 0);  //点的颜色 黄色
    painter.setPen(QPen(qcolor_round, 3, Qt::DotLine, Qt::RoundCap));
    size_t N = measurement_pack_list.size();
    if (N > 0) {
        for (size_t k = 0; k < N; ++k) {
            measurementGUI =
                    tools1.RadarPolarToGUI(measurement_pack_list[k].raw_measurements_);
            x = static_cast<int>(measurementGUI(0));
            y = static_cast<int>(measurementGUI(1));
            painter.drawPoint(x, y);
        }
    }

}

void MainWindow::plotTrustTracks(QPainter &painter, const std::vector<TrustTrackPackage> &trust_track_pack_list)
{
    int x;
    int y;
    VectorXd trackGUI;  //临时变量，存放转换到GUI上的坐标信息
    QColor qcolor_round_shi(220, 20, 60);  //实点的颜色
    painter.setPen(QPen(qcolor_round_shi, 6, Qt::DashDotLine, Qt::RoundCap));

    size_t N = trust_track_pack_list.size();
    for (size_t n = 0; n < N; ++n) {
        if (trust_track_pack_list[n].raw_track_data_output_(3) == 0) {  //说明是实点
            trackGUI = tools1.RadarPolarToGUI(
                        tools1.RadarCartesianToPolar(trust_track_pack_list[n].last_point_x_));
            x = static_cast<int>(trackGUI(0));
            y = static_cast<int>(trackGUI(1));
            painter.drawPoint(x, y);
        } else {
            QColor qcolor_round_bu(255, 255, 0);  //补点的颜色
            painter.setPen(QPen(qcolor_round_bu, 6, Qt::DashDotLine, Qt::RoundCap));
            trackGUI = tools1.RadarPolarToGUI(
                        tools1.RadarCartesianToPolar(trust_track_pack_list[n].last_point_x_));
            x = static_cast<int>(trackGUI(0));
            y = static_cast<int>(trackGUI(1));
            painter.drawPoint(x, y);
        }
    }

}

void MainWindow::paintEvent(QPaintEvent *event)
{/*
    Q_UNUSED(event);
    QPixmap pixmap(kRectangleWidth, kRectangleHigh);
    pixmap.fill(background_color);  //设置背景颜色
    QPainter painter(&pixmap);

    MainInterface(painter);  //静态界面
    //  @brief:雷达的测量值在此显示：
    //plotMeasurements(painter, m_measurement_pack_list);
    // plotMeasurements(painter,
    // _client.m_worker->track.measurement_pack_list_);
    // plotMeasurements(painter, track2.measurement_pack_list_);
    // plotMeasurements(painter, measurement_pack_list);

    // @brief 最终的航迹信息在此显示
    //plotTracks(painter,
    //_client.m_worker->track.track_data_output_pack_list_);


    plotTracks(painter,m_track_data_output_pack_list);

    //@brief 可靠航迹信息在此显示
    // plotTrustTracks(painter, track2.trust_track_pack_list_);

    // @brief:  动态扫描的部分在此显示

    static double MyScanLine;
    MyScanLine += static_cast<double>(2);
    if (MyScanLine >= 360.0) {
        MyScanLine = 0;
    }
    painter.translate(-kRoundDiameter / 2, -kRoundDiameter / 2);
    QConicalGradient gradient(kRoundDiameter / 2, kRoundDiameter / 2,
                              MyScanLine); gradient.setColorAt(0.2, QColor(47, 79, 79, 100));
    gradient.setColorAt(0.5, QColor(47, 79, 79, 0));
    painter.setBrush(gradient);
    painter.setPen(QPen(Qt::NoPen));
    painter.drawPie(0, 0, kRoundDiameter, kRoundDiameter, MyScanLine * 16.0,
                    90 * 16);

    //画图结束
    painter.end();
    pixmap.scaled(ui->label_show->size(), Qt::KeepAspectRatio);
    ui->label_show->setPixmap(pixmap);*/

}

void MainWindow::on_pushButton_14_clicked()
{

}

void MainWindow::on_pushButton_clicked()
{
    ui->plainTextEdit->clear();
}

void MainWindow::on_button_clearScreen_clicked()
{

}

void MainWindow::on_button_ipClearScreen_clicked()
{

}
